<?php

namespace app\nideshop\controller;

use think\Db;

class Index extends Base
{
	protected $datas;

	/**
	 * 首页数据
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-20T00:04:41+0800
	 * @return   [type]                   [description]
	 */
	public function indexAction()
	{
		$adData = Db::name('ad')
			->where(['ad_position_id' => 1, 'enabled' => 1])
			->select();
		$channelData = Db::name('channel')
			->order('sort_order', 'ASC')
			->select();
		$newGoodsData = Db::name('goods')
			->where('is_new', 1)
			->field('id,name,list_pic_url,retail_price')
			->limit(4)
			->select();
		$hotGoodsData = Db::name('goods')
			->where('is_hot', 1)
			->field('id,name,list_pic_url,retail_price,goods_brief')
			->limit(3)
			->select();
		$brandData = Db::name('brand')->where('is_new', 1)->order('new_sort_order', 'ASC')->limit(4)->select();	
		$topicData = Db::name('topic')->limit(3)->select();
		$categoryData = Db::name('category')
			->where('parent_id', 0)
			->where('name', '<>', '推荐')
			->select();
		$newCategoryList = [];
		foreach ($categoryData as $key => $val) {
			$categoryIds = Db::name('category')->where('parent_id', $val['id'])->column('id');
			$goodsData = Db::name('goods')
				->whereIn('category_id', $categoryIds)
				->field('id,name,list_pic_url,retail_price')
				->limit(7)
				->select();
			$newCategoryList[$key]['id'] = $val['id'];
			$newCategoryList[$key]['name'] = $val['name'];
			$newCategoryList[$key]['goodsList'] = $goodsData;	
		}	
		return json([
			'errno' => 0, 
			'errmsg' => '',
			'data' => [
				'banner' => $adData,
				'channel' => $channelData,
				'newGoodsList' => $newGoodsData,
				'hotGoodsList' => $hotGoodsData,
				'brandList' => $brandData,
				'topicList' => $topicData,
				'categoryList' => $newCategoryList
			]
		]);
	}
}